﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TinyEngine.Editor.Extensibility.Controls
{
    /// <summary>
    /// Interaction logic for RectangleControl.xaml
    /// </summary>
    public partial class RectangleControl : UserControl
    {
        public RectangleControl(string name)
        {
            InitializeComponent();
            lblName.Text = name;
        }
        private Microsoft.Xna.Framework.Rectangle? rectangle;
        public Microsoft.Xna.Framework.Rectangle? Rectangle
        {
            get { return rectangle; }
            set { rectangle = value;
            UpdateText();
            }
        }

        private void UpdateText()
        {
            if (!rectangle.HasValue)
            {
                txtX.Text = txtY.Text = txtW.Text = txtH.Text = "";
                chkEnabled.IsChecked = false;
                txtX.IsEnabled =
                    txtY.IsEnabled =
                    txtW.IsEnabled =
                    txtH.IsEnabled = false;
            }
            else
            {
                chkEnabled.IsChecked = true;
                txtX.IsEnabled =
                    txtY.IsEnabled =
                    txtW.IsEnabled =
                    txtH.IsEnabled = true;

                txtX.Text = rectangle.Value.X.ToString();
                txtY.Text = rectangle.Value.Y.ToString();
                txtW.Text = rectangle.Value.Width.ToString();
                txtH.Text = rectangle.Value.Height.ToString();
            }
        }

        private void txtY_TextChanged(object sender, TextChangedEventArgs e)
        {

            int xVal = 0;
            int yVal = 0;
            int wVal = 0;
            int hVal = 0;
            if (int.TryParse(txtX.Text, out xVal))
                if (int.TryParse(txtY.Text, out yVal))
                    if (int.TryParse(txtW.Text, out wVal))
                        if (int.TryParse(txtH.Text, out hVal))
                        {
                            rectangle = new Microsoft.Xna.Framework.Rectangle(xVal, yVal, wVal, hVal);
                            if (ValueChanged != null)
                                ValueChanged();
                        }

        }
        public event Action ValueChanged;

        private void chkEnabled_Checked(object sender, RoutedEventArgs e)
        {
            if (chkEnabled.IsChecked.HasValue && chkEnabled.IsChecked.Value)
            {
                rectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, 1, 1);
            }
            else
                rectangle = null;

            UpdateText();

            if (ValueChanged != null)
                ValueChanged();
        }
    }
}
